X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/2af83e98005a14c439b360a5b9ac636f594d9f0c..3de51c6f55d304f038df1b77c8ab346e2a187fe1:/Super%20Polarity/ActorManager.cs diff --git a/Super Polarity/ActorManager.cs b/Super Polarity/ActorManager.cs index 39ae336..f5587b9 100644 --- a/Super Polarity/ActorManager.cs +++ b/Super Polarity/ActorManager.cs @@ -9,10 +9,14 @@ namespace SuperPolarity { static class ActorManager { - static List Actors; + + static SuperPolarity Game; + static int OutlierBounds; + static IList Actors; static ActorManager() { + OutlierBounds = 100; Actors = new List(); } @@ -23,12 +27,15 @@ namespace SuperPolarity static public void CheckOut(Actor actor) { + actor.CleanUp(); Actors.Remove(actor); + actor = null; } static public void Update(GameTime gameTime) { CheckActors(); + CheckOutliers(); foreach (Actor actor in Actors) { actor.Update(gameTime); @@ -45,12 +52,22 @@ namespace SuperPolarity static void CheckActors() { - var i = 0; - foreach (Actor actor in Actors) + for (var i = Actors.Count - 1; i >= 0; i--) { - i++; - foreach (Actor other in Actors.Skip(i)) + if (i >= Actors.Count) { + i = Actors.Count - 1; + } + + if (Actors.Count == 0) { + return; + } + + Actor actor = Actors[i]; + for (var j = i - 1; j >= 0; j--) + { + Actor other = Actors[j]; + CheckCollision(actor, other); if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship))) @@ -63,6 +80,13 @@ namespace SuperPolarity static void CheckCollision(Actor actor, Actor other) { + var collision = Rectangle.Intersect(actor.Box, other.Box); + var inverseCollision = Rectangle.Intersect(other.Box, actor.Box); + if (!collision.IsEmpty && !actor.Dying && !other.Dying) + { + actor.Collide(other, collision); + other.Collide(actor, inverseCollision); + } } @@ -74,13 +98,63 @@ namespace SuperPolarity var dx = other.Position.X - actor.Position.X; var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); var angle = (float) Math.Atan2(dy, dx); + var otherAngle = (float)Math.Atan2(-dy, -dx); if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius) { actor.Magnetize(other, (float)linearDistance, angle); - other.Magnetize(actor, (float)linearDistance, 90 - angle); + other.Magnetize(actor, (float)linearDistance, otherAngle); + } + } + } + + static void CheckOutliers() + { + for (var i = Actors.Count; i > 0; i--) + { + var actor = Actors[i-1]; + if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds || + actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds || + actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds) + { + CheckOut(actor); + if (actor.Parent != null) + { + actor.Parent.Children.Remove(actor); + } + } + } + } + + static public void Empty() + { + foreach (Actor actor in Actors) { + actor.CleanUp(); + } + Actors.Clear(); + } + + internal static void SetGame(SuperPolarity game) + { + Game = game; + } + + public static void Bomb() + { + for (var i = Actors.Count - 1; i >= 0; i--) + { + var actor = Actors[i]; + if (actor.GetType() == typeof(StandardShip)) + { + CheckOut(actor); + Renderer.CheckOut(actor); } } } + + public static int CountBaddies() + { + return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count(); + } } }